package com.example.demothree.service.imp;

import com.example.demothree.bean.Employee;
import com.example.demothree.bean.Managers;
import com.example.demothree.bean.UserName;
import com.example.demothree.dao.EmployeeDao;
import com.example.demothree.dao.ManagersDao;
import com.example.demothree.dao.UserNameDao;
import com.example.demothree.service.XtService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class XtServiceImp implements XtService {
    @Resource
    private ManagersDao managersDao;
    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private UserNameDao userNameDao;

    private Managers managers;
    private Employee employee;
    @Override
    public Page<Employee> xtYhgl(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page,size);
        Page<Employee> pageyhgl = employeeDao.findAll(pageable);
        return pageyhgl;
    }
    //删除用户
    @Override
    public void deletekhzl(Long id) {
        employee = employeeDao.findBy(id);
        UserName userName = userNameDao.findUserNameByName(employee.getName());
        userNameDao.deleteById(userName.getId());
        employeeDao.deleteById(id);

    }
    //管理员分页
    @Override
    public Page<Managers> xtglygl(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page,size);
        Page<Managers> pageglygl = managersDao.findAll(pageable);
        return pageglygl;
    }
    //删除管理员
    @Override
    public void deletegly(Long id) {
        managers = managersDao.findBy(id);
        UserName userName = userNameDao.findUserNameByName(managers.getName());
        userNameDao.deleteById(userName.getId());
        managersDao.deleteById(id);
    }
    //添加管理员
    @Override
    public Managers addgy(Managers managers) {
        UserName userName = new UserName();
        userName.setName(managers.getName());
        userName.setManagersId(managers.getId());
        userNameDao.save(userName);
        return managersDao.save(managers);
    }
}
